package com.sdl.test.flutter_sdl_test.surface_view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.view.SurfaceHolder
import android.view.SurfaceView

class MySurfaceView(context: Context) : SurfaceView(context), SurfaceHolder.Callback {

    private var backgroundPaint: Paint = Paint().apply { color = Color.BLUE }

    init {
        holder.addCallback(this)
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        val canvas: Canvas? = holder.lockCanvas()
        canvas?.let {
            it.drawRect(0f, 0f, it.width.toFloat(), it.height.toFloat(), backgroundPaint)
            it.drawText("ssdfsdf", 0F, 0F, backgroundPaint)
            holder.unlockCanvasAndPost(it)
        }
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}

    override fun surfaceDestroyed(holder: SurfaceHolder) {}

    override fun setBackgroundColor(color: Int) {
        backgroundPaint.color = color
        val canvas: Canvas? = holder.lockCanvas()
        canvas?.let {
            it.drawRect(0f, 0f, it.width.toFloat(), it.height.toFloat(), backgroundPaint)
            holder.unlockCanvasAndPost(it)
        }
    }
}